import { loc, uJob, obj_str, d_sort, uDb, cline, for_obj } from 'sui';

const reload = (hDone, setMain, cur, mod="re_pair") => {
    uJob(hDone, "away/load",
        {
            line: cline(),
            tech: obj_str(cur, "sn"),
        },
        p => {
            const data = {};
            p.forEach(d => data[d.sn] = d);

            uDb(hDone, `file/read/${mod}_${obj_str(cur, "sn")}`,
                {
                    line: cline(),
                },
                r => {
                    const ret = [];

                    for_obj(r, (k, v) => {
                        const val = {};

                        const f = data[k];
                        if (f) {
                            for_obj(f, (tk, tv) => val[tk] = tv);
                        }

                        if (v) {
                            for_obj(v, (tk, tv) => val[tk] = tv);
                        }

                        ret.push(val);
                    });

                    d_sort(ret, "sn");
                    ret.forEach((d, i) => d["index"] = i + 1);

                    setMain(ret);
                }, e => setMain(null));
        }, e => setMain(null));
};

const fix_psn = () => {
    return [
        {sn: "n_b", name: `${loc("l_824")}${loc("l_748")}`, tp: "string"},
        {sn: "g_std", name: `${loc("l_823")}${loc("l_715")}`, tp: "string"},
        {sn: "g_sub", name: `${loc("l_823")}${loc("l_801")}`, tp: "string"},
        {sn: "g_dw", name: `${loc("l_823")}${loc("l_363")}`, tp: "string"},
    ];
};

export {fix_psn, reload};
